package com.ysxsoft.wdg.ui.activity.center_zy;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.home.ZyGoodsAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.zy.GoodsBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ZyCateListActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {

    public static void start(Context context, String title, String categoryid) {
        Intent intent = new Intent(context, ZyCateListActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("categoryid", categoryid);
        context.startActivity(intent);
    }

    public static void start(Context context, String title, String categoryid, boolean isCategory) {
        Intent intent = new Intent(context, ZyCateListActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("categoryid", categoryid);
        intent.putExtra("isCategory", isCategory);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.recyclerview2)
    RecyclerView recyclerview2;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private int page = 1;
    private ZyGoodsAdapter mAdapter;
    private int xu = 0;
    private String title = "", categoryid = "";
    private boolean isCategory=false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_zy_cate_list;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleIvR.setImageResource(R.mipmap.zy_search);

        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        categoryid = intent.getStringExtra("categoryid");
        isCategory = intent.getBooleanExtra("isCategory",false);

        titleContent.setText(title);
        tv0.setSelected(true);

        mAdapter = new ZyGoodsAdapter(R.layout.item_goods);
        recyclerview2.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        recyclerview2.setAdapter(mAdapter);


        multStatusView.showLoading();
        onRefresh(refreshLayout);

    }

    protected void setListener() {
        refreshLayout.setOnLoadmoreListener(this);
        refreshLayout.setOnLoadmoreListener(this);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GoodsDetailActivity.start(mContext, mAdapter.getItem(position).getId());
            }
        });
    }


    @OnClick({R.id.title_finish, R.id.title_iv_r})
    public void onViewClicked2(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.title_iv_r:
                ZySearchActivity.start(mContext);
                break;
        }
    }

    //xu  	1销量2最新3到手价倒序4到手价正序
    @OnClick({R.id.tv0, R.id.tv1, R.id.tv2, R.id.tv3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv0:
                setSelect(true, false, false, false);
                iv3.setImageResource(R.mipmap.xu_default);
                xu = 0;
                break;
            case R.id.tv1:
                setSelect(false, true, false, false);
                iv3.setImageResource(R.mipmap.xu_default);
                xu = 1;
                break;
            case R.id.tv2:
                setSelect(false, false, true, false);
                iv3.setImageResource(R.mipmap.xu_default);
                xu = 2;
                break;
            case R.id.tv3:
                setSelect(false, false, false, true);
                xu = (xu == 3) ? 4 : 3;
                iv3.setImageResource((xu == 3) ? R.mipmap.xu_down : R.mipmap.xu_up);
                break;
        }

        onRefresh(refreshLayout);
    }

    private void setSelect(boolean b, boolean b2, boolean b3, boolean b4) {
        tv0.setSelected(b);
        tv1.setSelected(b2);
        tv2.setSelected(b3);
        tv3.setSelected(b4);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadmore(500);
        page++;
        if(isCategory){
            getList2();
        }else{
            getList();
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.finishRefresh(500);

        page = 1;
        mAdapter.clear();
        if(isCategory){
            getList2();
        }else{
            getList();
        }
    }

    private void getList() {
        mApiHelper.getZyCateList(page + "", categoryid, xu + "", new Observer<BaseBean<List<GoodsBean>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<List<GoodsBean>> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    multStatusView.showContent();
                    if (page == 1) {
                        mAdapter.setNewData(baseBean.getData());
                    } else {
                        mAdapter.addData(baseBean.getData());
                    }
                } else {
                    if (page == 1) {
                        multStatusView.showEmpty();
                    } else {
                        multStatusView.showContent();
                        ToastUtils.showToast(baseBean.getMs());
                    }
                }

            }

            @Override
            public void onError(Throwable e) {
                multStatusView.showError();
            }

            @Override
            public void onComplete() {

            }
        });
    }
    private void getList2() {
        mApiHelper.getZyCateListByCategory(page + "", categoryid, xu + "", new Observer<BaseBean<List<GoodsBean>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<List<GoodsBean>> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    multStatusView.showContent();
                    if (page == 1) {
                        mAdapter.setNewData(baseBean.getData());
                    } else {
                        mAdapter.addData(baseBean.getData());
                    }
                } else {
                    if (page == 1) {
                        multStatusView.showEmpty();
                    } else {
                        multStatusView.showContent();
                        ToastUtils.showToast(baseBean.getMs());
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                multStatusView.showError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

}
